﻿
using System;
using System.Collections.Generic;
using Tau.Common.Utilities;
using Tau.DataAccess;
using Tau.Model;
using System.Linq;

namespace Tau.Service
{
    public partial class TauService
    {
        public Image AddImage(Image image, int largeImageWidth, int thumbnailImageWidth)
        {
            //Create thumbnail and large images
            image.LargeImage = ImageUtilities.ResizeImage(image.LargeImage, largeImageWidth);
            image.ThumbnailImage = ImageUtilities.ResizeImage(image.LargeImage, thumbnailImageWidth);
            image.CreatedDate = DateTime.Now;

            //map model to db object
            var adapter = new Adapters.Image();
            tblImage dbObject = adapter.GetDbObject(image);

            //perform the add
            _context.tblImages.AddObject(dbObject);
            _context.SaveChanges();

            //return model
            return adapter.GetModelObject(dbObject);
        }


        public Image GetImage(int id)
        {
            Image returnImage = null;

            tblImage image = _context.tblImages.FirstOrDefault(img => img.imageID == id);

            if (image != null)
            {
                returnImage = new Adapters.Image().GetModelObject(image);
            }

            return returnImage;
        }

        public List<Image> GetAllImages()
        {
            string category = string.Empty;

            //get first category
            if (_context.tblImages.Count() > 0)
            {
                var firstImage = _context.tblImages.First();
                category = firstImage.imageCategory;
            }
            else
            {
                List<Category> categories = GetAllCategories();
                if (categories.Count > 0)
                {
                    category = categories[0].CategoryName;
                }
            }

            return GetImagesByCategory(category);
        }

        public List<Image> GetImagesByCategory(string category)
        {
            List<tblImage> dbImageList = _context.tblImages.Where(i => i.imageCategory.ToLower() == category.ToLower()).ToList();

            return new Adapters.Image().GetModelList(dbImageList);
        }

        private bool IsImageInUse(int imageId)
        {
            if (_context.tblItems.Where(item => item.imageID == imageId).Count() > 0)
            {
                return true;
            }

            string imageIdString = string.Format("GetImage/{0}", imageId);

            if (_context.tblPages.Where(page => page.pageCode.Contains(imageIdString)).Count() > 0)
            {
                return true;
            }
            return false;
        }

        public void DeleteImage(int imageId)
        {
            if (IsImageInUse(imageId))
            {
                throw new Exception("This image is currently being used by a product or page, and cannot be deleted.");
            }
            tblImage imageToDelete = _context.tblImages.SingleOrDefault(img => img.imageID == imageId);
            _context.tblImages.DeleteObject(imageToDelete);
            _context.SaveChanges();
        }

        public void UpdateImage(int imageId, string newCategory)
        {
            tblImage imageToUpdate = _context.tblImages.SingleOrDefault(img => img.imageID == imageId);
            imageToUpdate.imageCategory = newCategory;
            _context.SaveChanges();
        }
    }
}
